package cn.sytton.taffecloud.service.job.controller;

import cn.sytton.taffecloud.common.base.doc.Api;
import cn.sytton.taffecloud.common.base.doc.ApiImplicitParam;
import cn.sytton.taffecloud.common.base.doc.ApiOperation;
import cn.sytton.taffecloud.common.base.doc.ParamType;
import cn.sytton.taffecloud.common.db.model.PageParam;
import cn.sytton.taffecloud.service.job.entity.JobEntity;
import cn.sytton.taffecloud.service.job.exception.TaskException;
import cn.sytton.taffecloud.service.job.service.IJobService;
import com.github.pagehelper.PageInfo;
import org.quartz.SchedulerException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 定时任务控制层
 *
 * @author skyrock
 */
@Api(tags = "定时任务")
@RestController
@RequestMapping
public class JobController {

    @Resource
    private IJobService jobService;

    @ApiOperation("查询分页定时任务列表")
    @ApiImplicitParam(name = "pp", value = "分页参数对象", paramType = ParamType.query, dataTypeClass = PageParam.class)
    @GetMapping
    public PageInfo<JobEntity> findListPage(PageParam pp) {
        return jobService.findListPage(pp);
    }

    @ApiOperation("查询单个定时任务")
    @ApiImplicitParam(name = "id", value = "定时任务ID", paramType = ParamType.path)
    @GetMapping("/{id}")
    public JobEntity findById(@PathVariable String id) {
        return jobService.findById(id);
    }

    @ApiOperation("添加定时任务")
    @ApiImplicitParam(name = "entity", value = "定时任务数据对象", paramType = ParamType.body, dataTypeClass = JobEntity.class)
    @PostMapping
    public void create(@RequestBody @Validated JobEntity entity) throws SchedulerException, TaskException {
        jobService.create(entity);
    }

    @ApiOperation("更新定时任务")
    @ApiImplicitParam(name = "entity", value = "定时任务数据对象", paramType = ParamType.body, dataTypeClass = JobEntity.class)
    @PostMapping("/update")
    public void update(@RequestBody @Validated JobEntity entity) throws SchedulerException, TaskException {
        jobService.update(entity);
    }

    @ApiOperation("删除定时任务")
    @ApiImplicitParam(name = "ids", value = "定时任务ID数组", paramType = ParamType.body, dataTypeClass = String[].class)
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) throws SchedulerException, TaskException {
        jobService.delete(ids);
    }

    @ApiOperation("修改定时任务状态")
    @ApiImplicitParam(name = "entity", value = "定时任务数据对象", paramType = ParamType.body, dataTypeClass = JobEntity.class)
    @PostMapping("/status/update")
    public void updateStatus(@RequestBody JobEntity entity) throws SchedulerException {
        jobService.updateStatus(entity);
    }

    @ApiOperation("定时任务立即执行一次")
    @ApiImplicitParam(name = "entity", value = "定时任务数据对象", paramType = ParamType.body, dataTypeClass = JobEntity.class)
    @PostMapping("/run")
    public void run(@RequestBody JobEntity entity) throws SchedulerException {
        jobService.run(entity);
    }

    @ApiOperation("查询任务运行统计信息")
    @ApiImplicitParam(name = "id", value = "定时任务名称", paramType = ParamType.path)
    @GetMapping("/run/info/{id}")
    public Map<String, Object> findRunInfo(@PathVariable String id) throws SchedulerException {
        return jobService.findRunInfo(id);
    }
}
